<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\UserSign;
use app\common\tool\Result;
use think\Response;

class Sign extends Base
{
    public function index(): Response
    {
        $param = $this->request->get();
        $date = $param['date'] ?: date('Y-m');
        $choice_time = strtotime($date);
        $now_time = strtotime(date('Y-m'));
        $sign_day = $this->user->sign_day;
        $always_day = $this->user->always_day;
        $is_day_sign = $this->user->is_day_sign;
        $sign_amount = $this->system['sign_give'];
        //获取某月天数
        $day = date('t', $choice_time);
        $month = [];
        if ($choice_time < $now_time) {
            for ($i = 1; $i <= $day; $i++) {
                //查询每日是否签到
                $eq = strlen(strval($i)) > 1 ? $i : '0' . $i;
                $sign_date = $date . '-' . $eq;
                $sign = UserSign::where(['uid' => $this->user->id, 'create_date' => $sign_date])->find();
                $month[] = [
                    'week' => getWeekTwo(date('w', strtotime($sign_date))),
                    'day' => $i,
                    'status' => $sign ? 1 : 0,
                    'amount' => $sign ? $sign->give_amount : '0'
                ];
            }
        } else {
            //今天
            $d = date('d');
            for ($i = 1; $i <= $day; $i++) {
                $eq = strlen(strval($i)) > 1 ? $i : '0' . $i;
                $sign_date = $date . '-' . $eq;
                //时间还没到
                if (date('Y-m') == $date) {
                    if ($i > $d) {
                        $month[] = [
                            'week' => getWeekTwo(date('w', strtotime($sign_date))),
                            'day' => $i,
                            'status' => 2,
                            'amount' => $sign_amount
                        ];
                    } elseif ($i == $d) {
                        $month[] = [
                            'week' => getWeekTwo(date('w', strtotime($sign_date))),
                            'day' => $i,
                            'status' => $is_day_sign,
                            'amount' => $sign_amount
                        ];
                    } else {
                        $sign = UserSign::where(['uid' => $this->user->id, 'create_date' => $sign_date])->find();
                        $month[] = [
                            'week' => getWeekTwo(date('w', strtotime($sign_date))),
                            'day' => $i,
                            'status' => $sign ? 1 : 0,
                            'amount' => $sign ? $sign->give_amount : '0'
                        ];
                    }
                } else {
                    $month[] = [
                        'week' => getWeekTwo(date('w', strtotime($sign_date))),
                        'day' => $i,
                        'status' => 2,
                        'amount' => $sign_amount
                    ];
                }
            }
        }

        $result = [
            'date' => date('Y年m月', strtotime($date)),
            'before' => date('Y-m', strtotime('-1 month', strtotime($date))),
            'after' => date('Y-m', strtotime("+1 month", strtotime($date))),
            'week' => ['日', '一', '二', '三', '四', '五', '六'],
            'month' => $month,
            'sign_day' => $sign_day,
            'always_day' => $always_day,
            'is_day_sign' => $is_day_sign
        ];

        return Result::success('success', $result);
    }

    public function add(): Response
    {
        return Result::fastReturn(UserSign::add($this->user, $this->system));
    }
}